package com.xzzz.irda.guardcat.server.repos.redis;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.xzzz.common.base.util.okhttp.HttpUtil;
import com.xzzz.common.base.util.json.JsonUtil;
import com.xzzz.irda.guardcat.core.constants.GuardcatConstant;
import com.xzzz.irda.guardcat.server.app.AppManager;
import com.xzzz.irda.guardcat.server.app.pojo.MachineInfo;
import com.xzzz.irda.guardcat.server.metric.MetricRepository;
import com.xzzz.irda.guardcat.server.metric.physical.PhysicalInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author xzzz
 */
@Slf4j
@Component
public class PhysicalRepositoryInRedis extends MetricRepository<PhysicalInfo> {

    private static final String PHYSICAL_INFO_KEY = "guardcat:physical:";

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    public AppManager appManager;

    @Override
    public void save(MachineInfo machineInfo, PhysicalInfo physicalInfo) {

    }

    @Override
    public void expire() {

    }

    @Override
    public PhysicalInfo getByMachineId(String machineId) {
        PhysicalInfo physicalInfo;
        // 缓存没有就去拉, 12小时拉一次
        String cache = redisTemplate.opsForValue().get(PHYSICAL_INFO_KEY + machineId);
        if (StrUtil.isNotBlank(cache)) {
            physicalInfo = JsonUtil.toObj(cache, PhysicalInfo.class);
            if (physicalInfo != null) {
                return physicalInfo;
            }
        }

        MachineInfo machineInfo = appManager.getMachine(machineId);

//        if (machineInfo.isNotProd()) {
//            log.info("非生产机器, 无法拉取数据");
//            return null;
//        }

        String systemUrl = buildUrl(machineInfo, GuardcatConstant.API.METRIC_SYSTEM_INFO);
        String jvmUrl = buildUrl(machineInfo, GuardcatConstant.API.METRIC_JVM_INFO);
        String system = HttpUtil.get(systemUrl);
        String jvm = HttpUtil.get(jvmUrl);

        JsonNode systemNode = JsonUtil.toJsonNode(system);
        JsonNode jvmNode = JsonUtil.toJsonNode(jvm);

        physicalInfo = new PhysicalInfo();
        physicalInfo.setAppName(machineInfo.getAppName());
        physicalInfo.setMachineId(machineId);

        physicalInfo.setUserName                  (systemNode.get("userName").asText());
        physicalInfo.setUserHome                  (systemNode.get("userHome").asText());
        physicalInfo.setSystemName                (systemNode.get("name").asText());
        physicalInfo.setSystemArchitecture        (systemNode.get("architecture").asText());
        physicalInfo.setSystemVersion             (systemNode.get("version").asText());
        physicalInfo.setSystemAvailableProcessors (systemNode.get("availableProcessors").asText());
        physicalInfo.setTotalPhysicalMemorySize   (systemNode.get("totalPhysicalMemorySize").asText());

        physicalInfo.setVnName                    (jvmNode.get("vmName").asText());
        physicalInfo.setJavaVersion               (jvmNode.get("javaVersion").asText());
        physicalInfo.setJavaHome                  (jvmNode.get("javaHome").asText());
        physicalInfo.setInputArguments            (jvmNode.get("inputArguments").asText());

        redisTemplate.opsForValue().set(PHYSICAL_INFO_KEY + machineId, JsonUtil.toJson(physicalInfo), 12, TimeUnit.HOURS);

        return physicalInfo;
    }

    @Override
    public List<PhysicalInfo> listByMachineId(String machineId) {
        return null;
    }
}
